/*
results:
no O3
Original method duration: 0.0169993 seconds
Optimized method duration: 0.00521096 seconds
Results are equal: Yes

O3
Original method duration: 0.00349086 seconds
Optimized method duration: 0.00113012 seconds
Results are equal: Yes
*/

#include <iostream>
#include <vector>
#include <chrono>
#include <numeric> // std::iota

int main() {
  const int num_detections = 1000000; // 假设我们有一百万个检测结果

  // 原始方法：逐一 emplace_back
  auto start = std::chrono::high_resolution_clock::now();
  std::vector<int> unmatched_detections_original;
  unmatched_detections_original.reserve(num_detections);
  for (int i = 0; i < num_detections; ++i) {
    unmatched_detections_original.emplace_back(i);
  }
  auto end = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> duration_original = end - start;
  std::cout << "Original method duration: " << duration_original.count()
            << " seconds" << std::endl;

  // 优化方法：使用 resize 和 std::iota
  start = std::chrono::high_resolution_clock::now();
  std::vector<int> unmatched_detections_optimized;
  unmatched_detections_optimized.resize(num_detections);
  std::iota(unmatched_detections_optimized.begin(),
            unmatched_detections_optimized.end(),
            0);
  end = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> duration_optimized = end - start;
  std::cout << "Optimized method duration: " << duration_optimized.count()
            << " seconds" << std::endl;

  // 验证结果是否相同
  bool is_equal =
      (unmatched_detections_original == unmatched_detections_optimized);
  std::cout << "Results are equal: " << (is_equal ? "Yes" : "No") << std::endl;

  return 0;
}
